/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.swirrel.installer;

import java.awt.Component;
import java.awt.TextComponent;
import java.awt.event.TextListener;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import org.swirrel.annotation.TextValueChanged;
import org.swirrel.listener.SwirrelTextListener;
import org.swirrel.util.MethodCall;

/**
 *
 * @author Gronau
 */
public class TextListenerInstaller extends AbstractListenerInstaller implements SwirrelListenerInstaller {

    public TextListenerInstaller() {
        super(Arrays.<Class<? extends Annotation>>asList(TextValueChanged.class),
                Arrays.<Class<?>>asList(TextComponent.class));
    }

    public boolean isListenerAlreadySet(Object obj) {
       TextListener[] textListeners = ((TextComponent) obj).getTextListeners();
        for (TextListener tl : textListeners) {
            if (tl instanceof SwirrelTextListener) {
                return true;
            }
        }
        return false;
    }

    public void registerListener(Field field, Component comp, java.util.List<MethodCall> methodCalls) throws IllegalAccessException {
        Object obj = field.get(comp);
        TextListener tl = new SwirrelTextListener(methodCalls.get(0));
         ((TextComponent) obj).addTextListener(tl);
    }
}
